import { getSearchHotList, getSearchKeyword, getSearchSuggest } from "../../service/request/search"

// pages/mainSearch/mainSearch.js
Page({
  data: {
    searchHotList: [],
    searchSuggest: [],
    searchSongs: [],
    keyword: "",
    isFocus: false
  },
  onLoad (){
    this.fetchSearchHotList()
  },
  // 获取热门搜索
  async fetchSearchHotList() {
    const res = await getSearchHotList()
    const searchHotList = res.result.hots
    this.setData({searchHotList})
  },
  // 热门搜索点击
  searchHotClick(event) {
    const value = event.detail
    this.setData({keyword: value})
    this.searchClick()
  },
  // 推荐搜索点击
  searchSuggestClick(event) {
    const value = event.detail
    this.setData({keyword: value})
    this.searchClick()
  },
  // 搜索聚焦
  searchFocus() {
    this.setData({isFocus: true, searchSongs: [], keyword: ""})
  },
  // 搜索失焦
  searchBlur() {
    this.setData({isFocus: false})
  },
  // 搜索建议
  async searchChange(event) {
    const value = event.detail
    this.setData({keyword: value})
    if(!value) {
      this.setData({searchSuggest: []})
      return
    }
    const res = await getSearchSuggest(value)
    const searchSuggest = res.result.songs
    this.setData({searchSuggest})
  },
  // 点击搜索按钮
  async searchClick() {
    const value = this.data.keyword
    if(!value)return
    const res = await getSearchKeyword(value)
    const searchSongs = res.result.songs
    this.setData({searchSongs})
  }
})